from django.utils.deprecation import MiddlewareMixin
from web import models
import datetime
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin

from django.conf import settings

class Tracer(object):

    def __int__(self):
        self.user = None
        self.price_policy = None
        self.project = None


class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        """ 如果用户已登录，则request中赋值 """

        request.tracer = Tracer()

        user_id = request.session.get('user_id', 0)
        user_object = models.UserInfo.objects.filter(id=user_id).first()
        request.tracer.user = user_object

        # 白名单：没有登录都可以访问的URL
        """
        1. 获取当用户访问的URL
        2. 检查URL是否在白名单中，如果再则可以继续向后访问，如果不在则进行判断是否已登录
        """
        if request.path_info in settings.WHITE_REGEX_URL_LIST:
            return

        # 检查用户是否已登录，已登录继续往后走；未登录则返回登录页面。
        if not request.tracer.user:
            return redirect('login')

        # 登录成功之后，访问后台管理时：获取当前用户所拥有的额度

        # 方式一：免费额度在交易记录中存储

        # 获取当前用户ID值最大（最近交易记录）
        _object = models.Transaction.objects.filter(user=user_object, status=2).order_by('-id').first()
        # 判断是否已过期
        current_datetime = datetime.datetime.now()
        if _object.end_datetime and _object.end_datetime < current_datetime:
            _object = models.Transaction.objects.filter(user=user_object, status=2, price_policy__category=1).first()

        request.tracer.price_policy = _object.price_policy

        # 方式二：免费的额度存储配置文件
        """
        # 获取当前用户ID值最大（最近交易记录）
        _object = models.Transaction.objects.filter(user=user_object, status=2).order_by('-id').first()

        if not _object:
            # 没有购买
            request.price_policy = models.PricePolicy.objects.filter(category=1, title="个人免费版").first()
        else:
            # 付费版
            current_datetime = datetime.datetime.now()
            if _object.end_datetime and _object.end_datetime < current_datetime:
                request.price_policy = models.PricePolicy.objects.filter(category=1, title="个人免费版").first()
            else:
                request.price_policy = _object.price_policy
        """

    def process_view(self, request, view, args, kwargs):

        # 判断URL是否是以manage开头，如果是则判断项目ID是否是我创建 or 参与
        if not request.path_info.startswith('/manage/'):

            return

        project_id = kwargs.get('project_id')

        # 是否是我创建的
        project_object = models.Project.objects.filter(creator=request.tracer.user, id=project_id).first()
        if project_object:
            # 是我创建的项目的话，我就让他通过
            request.tracer.project = project_object

            return

        # 是否是我参与的项目
        project_user_object = models.ProjectUser.objects.filter(user=request.tracer.user, project_id=project_id).first()
        if project_user_object:
            # 是我参与的项目
            request.tracer.project = project_user_object.project

            return

        return redirect('project_list')



#
# class AuthMiddleware(MiddlewareMixin):
#
#     def process_request(self,request):
#         """ 如果用户 已登录，则request中赋值 """
#
#         request.tracer = Tracer()
#
#         user_id = request.session.get('user_id',0)
#         user_object = models.UserInfo.objects.filter(id=user_id).first()
#
#         #中间件里边的     request.tracer.user        这个操作之后  所有的   request 都会携带  tracer.user
#         request.tracer.user = user_object
#
#         # 白名单：没有登录都可以访问的URL
#         """
#         1. 获取当用户访问的URL
#         2. 检查URL是否在白名单中，如果再则可以继续向后访问，如果不在则进行判断是否已登录
#         """
#         #之前的
#         # if request.path_info in settings.WHITE_REGEX_URL_LIST:
#         #     return
#
#         if request.path not in settings.WHITE_REGEX_URL_LIST:  # 假设你有一个WHITELIST_URLS设置
#             if not request.tracer.user:
#                 return redirect('login')  # 用户未登录，重定向到登录页面
#
#         #     # 检查用户是否已登录，已登录继续往后走；未登录则返回登录页面。
#         # if not request.tracer.user:
#         #     return redirect('login')
#
#         #     # 登录成功之后，访问后台管理时：获取当前用户所拥有的额度
#         #
#         #     # 方式一：免费额度在交易记录中存储
#         #
#         #     # 获取当前用户ID值最大（最近交易记录）
#         # _object = models.Transaction.objects.filter(user=user_object, status=2).order_by('-id').first()
#         # # 判断是否已过期
#         # current_datetime = datetime.datetime.now()
#         # if _object.end_datetime and _object.end_datetime < current_datetime:
#         #     _object = models.Transaction.objects.filter(user=user_object, status=2, price_policy__category=1).first()
#         #
#         # request.tracer.price_policy = _object.price_policy
#
#         # 检查用户是否拥有有效的交易记录
#         _object = models.Transaction.objects.filter(
#             user=user_object,
#             status=2
#         ).order_by('-id').first()
#
#         if _object and _object.end_datetime and _object.end_datetime < datetime.datetime.now():
#             _object = models.Transaction.objects.filter(
#                 user=user_object,
#                 status=2,
#                 price_policy__category=1
#             ).first()
#
#         request.tracer.price_policy = getattr(_object, 'price_policy', None)  # 使用getattr避免NoneType错误
#
#
#     def process_view(self,request,view,args,kwargs):
#
#         """ 判断URL是否是以manage开头
#
#             project_id 是我创建 or 我参与的
#         """
#
#         # #判断url是否已manage开头，如果是则判断项目ID是否是我创建的 or 参与
#         # if request.path_info.startswith("/manage/"):
#         #     return
#         # project_id = kwargs.get('project_id')
#
#         # 判断URL是否是以manage开头
#         if request.path.startswith("/manage/"):
#             project_id = kwargs.get('project_id')
#             if not project_id:
#                 return redirect('project_list')  # 如果没有project_id，重定向到项目列表页面
#
#         project_id = kwargs.get('project_id')
#         # #request.tracer.user
#         # #是否是我创建的
#         # project_object = models.Project.objects.filter(
#         #     creator=request.tracer.user,
#         #     id=project_id
#         # ).first()
#         #
#         # if project_object:
#         #     #是我创建的项目 ， 就通过
#         #     request.tracer.project = project_object
#         #     return
#         #
#         # #是我参与的项目
#         # project_object = models.ProjectUser.objects.filter(user=request.tracer.user,project_id=project_id).first()
#         # if project_object:
#         #     #是我参与的项目
#         #     # request.tracer.project = project_user_object.project
#         #     return
#         #
#         #
#         # # 不是我创建的，也不是我参与的项目，则跳转到项目列表页面
#         # return redirect('project_list')
#
#         # 检查项目是否是用户创建的或者用户参与的
#         project_object = models.Project.objects.filter(
#             creator=request.tracer.user,
#             id=project_id
#         ).first()
#
#         if not project_object:
#             project_object = models.ProjectUser.objects.filter(
#                 user=request.tracer.user,
#                 project_id=project_id
#             ).first()
#
#         if not project_object:
#             return redirect('project_list')  # 如果既不是创建者也不是参与者，重定向到项目列表页面
#
#         request.tracer.project = project_object